* ESTIMATION OF QUANTILE REG REDUCED FORMS

* DEP VAR ARE:  EXPECTED INFLATION - AT 6M, 12M, 24M, 3-5YRS;
*		LOG LAB DEMAND - AT T+1, T+2, T+3, T+4
*		OWN PRICE ANNUAL CHANGE - EXPECTED AT 12M, OBSERVED AT T+1, T+2, T+4


* ESTIMATE SAME MODEL IN 2 WAYS: WITH AND WITHOUT SAMPLE WEIGHTS.
* TSTATS OF ALL COEFF ESTIMATES FROM UNWEIGHTED QREGS ARE REPORTED IN FIG 5 MAIN TEXT
* COEFF AND 95%CI FOR EXPECTED INFALTION AT 12M REPORTED IN FIG 6 MAIN TEXT AND FOR ALL HORIZONS IN FIGS E1-E4.


* COEFFICIENTS AND SES ARE STORED IN MATRIXES (EB, ES - EXP INFL; PB, PS - OWN PRICE; LB, LS- LAB DEMAND)
* MATRIX ROWS=PERCENTILES, MATRIX COLUMNS=QUARTERS
* MATRIXES ARE THEN SAVED AS DTA AND USED TO GENERATE FIGURES



set more off

use WorkingData, clear

local explvar=" dpre i.dim i.area i.setton5 "

* ROWS=PERCENTILES, COLUMNS=QUARTERS
mat def XX = J(19, 42, .)


* WEIGHTED QREGS

quietly{

mat def EB6=XX
mat def ES6=XX
mat def EB12=XX
mat def ES12=XX
mat def EB24=XX
mat def ES24=XX
mat def EB48=XX
mat def ES48=XX


mat def PB0=XX
mat def PS0=XX
mat def PB1=XX
mat def PS1=XX
mat def PB2=XX
mat def PS2=XX
mat def PB4=XX
mat def PS4=XX

mat def LB1=XX
mat def LS1=XX
mat def LB2=XX
mat def LS2=XX
mat def LB3=XX
mat def LS3=XX
mat def LB4=XX
mat def LS4=XX


tokenize "EB6 ES6 EB12 ES12 EB24 ES24 EB48 ES48 PB0 PS0 PB1 PS1 PB2 PS2 PB4 PS4 LB1 LS1 LB2 LS2 LB3 LS3 LB4 LS4"

local db = 1
local ds = 2

foreach y in it6all it12all it24all it48all dprez F1DPZ F2DPZ F4DPZ F1LAD F2LAD F3LAD F4LAD{

	local c = 1
	forvalues t=210/251{
		
		local r = 1
		forvalues q=5(5)95{
			
			cap qui qreg $_y treatment $_explvar [pw=peso] if dp==$_t, q($_q)
			if _rc==0{
			mat `$_db'[$_r,$_c] = _b[treat]
			mat `$_ds'[$_r,$_c] = _se[treat]
			}
			
			local r = $_r + 1
		}
	
	local c = $_c + 1
	}
	
	local db = $_db + 2
	local ds = $_ds + 2
}


* store results in dta	


	foreach z in EB6 ES6 EB12 ES12 EB24 ES24 EB48 ES48 PB0 PS0 PB1 PS1 PB2 PS2 PB4 PS4 LB1 LS1 LB2 LS2 LB3 LS3 LB4 LS4{
		drop _all
		svmat $_z
		gen q = 5 + (_n-1)*5
		reshape long $_z, i(q) j(t)
		replace t = 209 + t
		if "$_z"!="EB6"{
			merge 1:1 q t using QRegRedFormResults
			drop _m
		}
		save QRegRedFormResults, replace
	}


}

	
** UNWEIGHTED QREGS
		

set more off

use WorkingData, clear

local explvar=" dpre i.dim i.area i.setton5 "

* ROWS=PERCENTILES, COLUMNS=QUARTERS
mat def XX = J(19, 42, .)

quietly{

mat def EB6=XX
mat def ES6=XX
mat def EB12=XX
mat def ES12=XX
mat def EB24=XX
mat def ES24=XX
mat def EB48=XX
mat def ES48=XX


mat def PB0=XX
mat def PS0=XX
mat def PB1=XX
mat def PS1=XX
mat def PB2=XX
mat def PS2=XX
mat def PB4=XX
mat def PS4=XX

mat def LB1=XX
mat def LS1=XX
mat def LB2=XX
mat def LS2=XX
mat def LB3=XX
mat def LS3=XX
mat def LB4=XX
mat def LS4=XX


tokenize "EB6 ES6 EB12 ES12 EB24 ES24 EB48 ES48 PB0 PS0 PB1 PS1 PB2 PS2 PB4 PS4 LB1 LS1 LB2 LS2 LB3 LS3 LB4 LS4"

local db = 1
local ds = 2



foreach y in it6all it12all it24all it48all dprez F1DPZ F2DPZ F4DPZ F1LAD F2LAD F3LAD F4LAD{

	local c = 1
	forvalues t=210/251{
		
		local r = 1
		forvalues q=5(5)95{
			
			cap qui qreg $_y treatment $_explvar if dp==$_t, q($_q)
			if _rc==0{
			mat `$_db'[$_r,$_c] = _b[treat]
			mat `$_ds'[$_r,$_c] = _se[treat]
			}
			
			local r = $_r + 1
		}
	
	local c = $_c + 1
	}
	
	local db = $_db + 2
	local ds = $_ds + 2
}

	
* store results in dta

	foreach z in EB6 ES6 EB12 ES12 EB24 ES24 EB48 ES48 PB0 PS0 PB1 PS1 PB2 PS2 PB4 PS4 LB1 LS1 LB2 LS2 LB3 LS3 LB4 LS4{
		drop _all
		svmat $_z
		gen q = 5 + (_n-1)*5
		reshape long $_z, i(q) j(t)
		replace t = 209 + t
		if "$_z"!="EB6"{
			merge 1:1 q t using UQRegRedFormResults
			drop _m
		}
		save UQRegRedFormResults, replace
	}

}

* ********************************* GENERATE FIG5, FIG 6, FIGS E1-E4  (FIG 6 == FIG E2 - exp infl 12 months) ***************************************


* Fig 5 SUMMARY OF TSTATS of QRegs

quietly{
use UQRegRedFormResults, clear

local i=1
foreach x in 0 1 2 4{
gen TP$_i = PB$_x/PS$_x
local i = $_i + 1
}
local i=1
forvalues l=1/4{
gen TL$_i=LB$_l/LS$_l
local i = $_i + 1
}
local i = 1
 foreach x in 6 12 24 48{
gen TE$_i=EB$_x/ES$_x
local i = $_i + 1
}
keep q t T*
reshape long TP TL TE, i(q t) j(h)
reshape long T, i(q t h) j(v, string)

gen b=.
replace b=1 if T<-3.5
local i = 2
forvalues j=-3.5(0.2)3.5{
replace b=$_i if T>=$_j & T<`=$_j+0.2'
local i =$_i + 1
}
di $_i
replace b=$_i if T>=3.5 & T!=.

sort v h b
egen Z=count(T), by(v h b)
sort v h b
by  v h b: gen x=_n==1
gen Z1=Z*x
egen TT=sum(Z1), by(v h)
gen S=100*Z1/TT

egen G=group(v h)

cap lab drop lg
lab def lg 1 "6 months" 2 "1 year" 3 "2 years" 4 "3-5 years" 5 "observed at t+1" 6 "observed at t+2" 7 "observed at t+3" 8 "observed at t+4" 9 "expected at t, 1 year" 10 "observed at t+1" 11 "observed at t+2" 12 "observed at t+4"
lab val G lg

gen z = -3.6 + 0.2 * (b-1)
format z %3.1f
twoway bar S z if x==1 & z>=-3.6 & z<=3.6, barw(0.2) by(G, im(zero) note("") graphregion(c(white)) row(3)) subti(,bc(white)) xla(-3.5(0.5)3.5,  angle(270) labsi(*0.7)) xsize(12) ysize(10) yti("") xti("t-statistic") yla(0(10) 50,labsi(*0.7) angle(0)) xli(-2 +2) xscale(range(-3.6 +3.6))

gr_edit .plotregion1.r2title[4]._set_orientation rvertical
gr_edit .plotregion1.r2title[4].text = {}
gr_edit .plotregion1.r2title[4].text.Arrpush Expected inflation at t
gr_edit .plotregion1.r2title[8].text = {}
gr_edit .plotregion1.r2title[8].text.Arrpush Labor demand
gr_edit .plotregion1.r2title[12].text = {}
gr_edit .plotregion1.r2title[12].text.Arrpush Annual price change
graph export Fig5.eps, replace
}

* ===================================
* PRINT TO LOG DATA USED FOR FIG5
* S is share of tstats from red form of depvar V at horizon H in bin B 
* see above how to construct fig
sort G z
* *********************************
* ***** DATA FOR FIG 5 ************
* *********************************
list v h G S z b if x==1 & z>=-3.6 & z<=3.6, linesize(255) header noobs
* =============================================

* FIGs OF QR COEFF FOR EXPECTED INFL - Fig 6 and Figs E1-E4

use UQRegRedFormResults, clear

quietly{
keep q t EB* ES*

foreach x in 6 12 24 48{
gen le$_x= EB$_x - 1.96*ES$_x
gen ue$_x= EB$_x + 1.96*ES$_x
}

* ***************************
* THIS ADDS DATA TO LOCATE INFLATION RATE PRESENTED TO INFORMED FIRMS IN PLOT
local nqs=rowsof(REFINFL_NOW)
local c=2
foreach z in 6 12 24 48{
	gen qinfl$_z=.
	forvalues r=1/$_nqs{
		replace qinfl$_z = REFINFL_NOW[$_r,$_c] if q==5 & t==209+$_r
	}
	local c=$_c+1
}
* *******************

* THIS IS NEEDED TO FORMAT GRAPH
gen hh=4
gen ll=-2

gen dp = t
format dp %tq

gen year=yofd(dofq(dp))
gen quarter=quarter(dofq(dp))

egen QQ=concat(year quarter), punct(":")
egen C=group(dp)
labmask C, val(QQ)
gen zero=0

#delimit;
twoway rbar le12 ue12 q  if q>5 & q<95, col(black) || line zero q if q>5 & q<95, lc(gs13) 
|| rbar hh ll qinfl12, barw(0.1) lc(black) lp(shortdash)
by(C, im(zero) legend(off) note("") graphregion(c(white)) )  subti(,bc(white) ) xsize(20) ysize(20)
yaxis(1 2) yla(-2(1)4,angle(0)) yla(-2(1)4, angle(0) axis(2)) xla(10 25 50 75 90 , labsi(*0.9) grid)  
xti("Percentiles of expected inflation", si(*0.8)) yti("95% confidence intervals", si(*0.8));
*graph export CI95_QR_E12.eps, replace;

graph export FigE2.eps, replace;
graph export Fig6.eps, replace;

#delimit;
twoway rbar le6 ue6 q  if q>5 & q<95, col(black) || line zero q if q>5 & q<95, lc(gs13) 
|| rbar hh ll qinfl6, barw(0.1) lc(black) lp(shortdash)
by(C, im(zero) legend(off) note("") graphregion(c(white)) )  subti(,bc(white) ) xsize(20) ysize(20)
yaxis(1 2) yla(-2(1)4,angle(0)) yla(-2(1)4, angle(0) axis(2)) xla(10 25 50 75 90 , labsi(*0.9) grid)  
xti("Percentiles of expected inflation", si(*0.8)) yti("95% confidence intervals", si(*0.8));
*graph export CI95_QR_E6.eps, replace;
graph export FigE1.eps, replace;

#delimit;
twoway rbar le24 ue24 q  if q>5 & q<95, col(black) || line zero q if q>5 & q<95, lc(gs13) 
|| rbar hh ll qinfl24, barw(0.1) lc(black) lp(shortdash)
by(C, im(zero) legend(off) note("") graphregion(c(white)) )  subti(,bc(white) ) xsize(20) ysize(20)
yaxis(1 2) yla(-2(1)4,angle(0)) yla(-2(1)4, angle(0) axis(2)) xla(10 25 50 75 90 , labsi(*0.9) grid)  
xti("Percentiles of expected inflation", si(*0.8)) yti("95% confidence intervals", si(*0.8));
*graph export CI95_QR_E24.eps, replace;
graph export FigE3.eps, replace;

#delimit;
twoway rbar le48 ue48 q  if q>5 & q<95, col(black) || line zero q if q>5 & q<95, lc(gs13) 
|| rbar hh ll qinfl48, barw(0.1) lc(black) lp(shortdash)
by(C, im(zero) legend(off) note("") graphregion(c(white)) )  subti(,bc(white) ) xsize(20) ysize(20)
yaxis(1 2) yla(-2(1)4,angle(0)) yla(-2(1)4, angle(0) axis(2)) xla(10 25 50 75 90 , labsi(*0.9) grid)  
xti("Percentiles of expected inflation", si(*0.8)) yti("95% confidence intervals", si(*0.8));
*graph export CI95_QR_E48.eps, replace;
graph export FigE4.eps, replace;

#delimit cr

}

* ====================================================================================
* THIS PRINTS TO LOG DATA USED FOR ABOVE FIGURES
* 95CI FOR EXP INFL AT ALL HORIZONS 
* UPPER (ueX) AND LOWER (leX) BOUND OF 95% CI of QR ESTIMATE OF DUMMY INFO ASSIGNMENT AT QUANTILE Q IN QUARTER C ON EXPECTED INFLATION AT HORIZON XX
sort C q
list C q le6 ue6 le12 ue12 le24 ue24 le48 ue48 if q>5 & q<95, linesize(255) header noobs
* ====================================================================================
